package com.maezia.tdr.controller;

import com.maezia.tdr.common.Result;
import com.maezia.tdr.common.annotation.Loggable;
import com.maezia.tdr.common.enums.StatusCodeEnum;
import com.maezia.tdr.common.exception.DriveException;
import com.maezia.tdr.domain.vo.AccessTokenVo;
import com.maezia.tdr.service.AccessTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "AccessToken相关接口",tags = "AccessToken管理")
@RestController
public class AccessTokenController {

    @Autowired
    private AccessTokenService accessTokenService;

    @Loggable
    @ApiOperation(value="获取AccessToken接口", httpMethod= "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name="appKey",value="应用标识",dataType="string"),
            @ApiImplicitParam(name="appSecret",value="应用秘钥",dataType="string")})
    @GetMapping("/get-token")
    public Result<AccessTokenVo> getAccessToken(@RequestParam(required = false) String appKey,
                                                @RequestParam(required = false) String appSecret) throws DriveException {

        if(StringUtils.isEmpty(appKey)){
            throw new DriveException(StatusCodeEnum.CLIENT_ID_NOT_NULL);
        }

        if(StringUtils.isEmpty(appSecret)){
            throw new DriveException(StatusCodeEnum.CLIENT_SECRET_NOT_NULL);
        }

        AccessTokenVo tokenVo = accessTokenService.getAccessToken(appKey, appSecret);
        return Result.success(tokenVo);
    }
}
